<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
                :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="mb-[10px]" v-show="pd.showSearch">
        <el-card shadow="hover">
          <el-form :model="pd.queryParams" ref="queryFormRef" :inline="true" label-width="68px">
            <el-form-item label="名称" prop="name">
              <el-input v-model="pd.queryParams.name" placeholder="请输入名称" clearable style="width: 240px"
                        @keyup.enter="handleQuery"/>
            </el-form-item>
            <el-form-item label="状态" prop="status">
              <el-switch v-model="pd.queryParams.status" active-text="停用" inactive-text="正常"
                         active-value="1" inactive-value="0"></el-switch>
            </el-form-item>
            <el-form-item label="值" prop="remark">
              <el-input v-model="pd.queryParams.remark" placeholder="请输入值" clearable style="width: 240px"
                        @keyup.enter="handleQuery"/>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="hover">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['demo:demo3:add']">新增
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="pd.single" @click="handleUpdate()"
                       v-hasPermi="['demo:demo3:edit']">修改
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="pd.multiple" @click="handleDelete()"
                       v-hasPermi="['demo:demo3:remove']">删除
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['demo:demo3:export']">导出
            </el-button>
          </el-col>
          <right-toolbar v-model:showSearch="pd.showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="pd.loading" :data="pd.demo3List" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="主键" align="center" prop="id" v-if="true"/>
        <el-table-column label="名称" align="center" prop="name">
          <template #default="scope">
            <partner :demo3="scope.row"/>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="status">
          <template #default="scope">
            <dict-tag :options="sys_status" :value="scope.row.status"/>
          </template>
        </el-table-column>
        <el-table-column label="值" align="center" prop="remark">
          <template #default="scope">
            <Dept :demo3="scope.row"/>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                         v-hasPermi="['demo:demo3:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                         v-hasPermi="['demo:demo3:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <Pagination v-show="pd.total > 0" :total="pd.total" v-model:page="pd.queryParams.pageNum"
                  v-model:limit="pd.queryParams.pageSize" @pagination="getList"/>
    </el-card>
    <!-- 添加或修改测试单对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="demoFormRef" :model="pd.form" :rules="pd.rules" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="pd.form.name" placeholder="请输入名称"/>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="pd.form.status" placeholder="请选择状态">
            <el-option v-for="dict in sys_status" :key="dict.value"
                       :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="值" prop="remark">
          <el-input v-model="pd.form.remark" placeholder="请输入值"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="pd.buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Demo3" lang="ts">
import {listDemo3, getDemo3, addDemo3, updateDemo3, delDemo3} from '@/api/demo/demo3';
import {Demo3Form, Demo3Query, Demo3VO} from '@/api/demo/demo3/types';
import Partner from "@/views/demo/demo3/partner.vue"
import Dept from "@/views/demo/demo3/dept.vue"

const {proxy} = getCurrentInstance() as ComponentInternalInstance;
const {sys_status} = toRefs<any>(proxy?.useDict('sys_status'));

const queryFormRef = ref<ElFormInstance>();
const demoFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const pd = reactive({
  demo3List: <Demo3VO[]>([]),
  buttonLoading: false,
  loading: true,
  showSearch: true,
  ids: <Array<string | number>>([]),
  single: true,
  multiple: true,
  total: 0,
  form: <Demo3Form>{},
  queryParams: <Demo3Query>{
    pageNum: 1,
    pageSize: 10,
    name: undefined,
    status: undefined,
    remark: undefined,
  },
  rules: <ElFormRules>{
    name: [      {required: true, message: "名称不能为空", trigger: "blur"}    ],
    status: [      {required: true, message: "状态不能为空", trigger: "blur"}    ],
    remark: [      {required: true, message: "值不能为空", trigger: "blur"}    ],
  }
});

/** 查询测试单列表 */
async function getList() {
  pd.loading = true;
  const res = await listDemo3(pd.queryParams);
  pd.demo3List = res.rows;
  pd.total = res.total;
  pd.loading = false;
}

/** 取消按钮 */
function cancel() {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
function reset() {
  pd.form = {
    id: undefined,
    name: undefined,
    status: undefined,
    remark: undefined,
  };
  demoFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
function handleQuery() {
  pd.queryParams.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection: Demo3VO[]) {
  pd.ids = selection.map(item => item.id);
  pd.single = selection.length != 1;
  pd.multiple = !selection.length;
}

/** 新增按钮操作 */
async function handleAdd() {
  reset();
  dialog.visible = true;
  dialog.title = "添加测试单";
}

/** 修改按钮操作 */
async function handleUpdate(row?: Demo3VO) {
  reset();
  const _id = row?.id || pd.ids[0]
  const res = await getDemo3(_id);
  Object.assign(pd.form, res.data);
  console.log(pd.form)
  dialog.visible = true;
  dialog.title = "修改测试单";
}

/** 提交按钮 */
function submitForm() {
  demoFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      pd.buttonLoading = true;
      if (pd.form.id) {
        await updateDemo3(pd.form).finally(() => pd.buttonLoading = false);
      } else {
        await addDemo3(pd.form).finally(() => pd.buttonLoading = false);
      }
      proxy?.$modal.msgSuccess("修改成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
async function handleDelete(row?: Demo3VO) {
  const _ids = row?.id || pd.ids;
  await proxy?.$modal.confirm('是否确认删除测试单编号为"' + _ids + '"的数据项？').finally(() => pd.loading = false);
  await delDemo3(_ids);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
function handleExport() {
  proxy?.download('demo/demo/export', {
    ...pd.queryParams
  }, `demo_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});

</script>
